package com.example.pathfake;

import java.util.ArrayList;

import org.joda.time.DateTime;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.fake.path.adapter.BaseClockAdapter;
import com.fakepath.helpers.Clock;
import com.fakepath.items.ItemPath;
import com.fakepath.views.ObservableListView;

public class MainActivity extends Activity {
	private Clock clockScrollerHelper;
	private View clockView;
	private final Handler handler = new Handler();
	protected DateTime now;
	private BaseClockAdapter adapter;
	private ObservableListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pathfeedactivity);
		clockView = this.findViewById(R.id.clock);
		this.now = DateTime.now();

		this.clockScrollerHelper = new Clock(this.clockView) {
			protected Handler getHandler() {
				return MainActivity.this.handler;
			}

			protected DateTime getNow() {
				return MainActivity.this.now;
			}

			@Override
			public void onUpdateClock() {
				MainActivity.this.handler.post(updateClockScrollerTask);
			}

		};

		initListView();
	}

	private final Runnable updateClockScrollerTask = new Runnable() {
		public void run() {
			ObservableListView localObservableListView = MainActivity.this.listView;
			if (localObservableListView == null)
				return;
			int i = localObservableListView
					.getItemIndexAtLocation(MainActivity.this.clockScrollerHelper
							.getTargetLocation());
			if ((i < 0) || (i >= MainActivity.this.adapter.getCount())) {
				MainActivity.this.clockScrollerHelper.hide();
				return;
			}
			ItemPath itemPath = (ItemPath) MainActivity.this.listView
					.getItemAtPosition(i);
			MainActivity.this.now = itemPath.getDateTime();
			MainActivity.this.clockScrollerHelper.update(MainActivity.this.now);
		}
	};

	private void initListView() {
		ArrayList<ItemPath> arrayList = new ArrayList<ItemPath>();
		String[] test = new String[10];
		for (int i = 0; i < 10; i++) {
			ItemPath itemPath = new ItemPath();
			test[i] = itemPath.getTitle();
			arrayList.add(itemPath);
		}

		this.listView = (ObservableListView) this
				.findViewById(R.id.observableListView1);
		adapter = new BaseClockAdapter(getApplicationContext(), listView,
				arrayList);
		this.listView.setAdapter(adapter);
		this.listView.setOnScrollBarChangePosition(clockScrollerHelper);
	}

}
